package org.androbuzz;

import java.net.MalformedURLException;
import java.net.URL;

import org.androbuzz.AndroBuzzSAXHandler.Feed;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.location.Location;
import android.location.LocationManager;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;

public class AndroBuzzNearbyActivity extends Activity {
	public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ProgressDialog dialog = ProgressDialog.show(AndroBuzzNearbyActivity.this, "", 
                "Locating yourself. Please wait...", true);
        dialog.show();
        // Getting location information
        LocationManager locator = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        // Try to get it via the device's GPS
        Location l = locator.getLastKnownLocation(LocationManager.GPS_PROVIDER);
        if (l == null) {
          // Fall back to coarse location.
          l = locator.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
        }
        TextView textview = new TextView(this);
        // So far : display a message with coordinates
        if(l != null){
        	double latitude = l.getLatitude();
        	double longitude = l.getLongitude();
        	AndroBuzzSAXHandler mSAXHandler = new AndroBuzzSAXHandler();
			URL feedUrl;
			try {
				feedUrl = new URL("https://www.googleapis.com/buzz/v1/activities/search?lat="+latitude+"&lon="+longitude+"&radius=5000&alt=atom&max-results=200");
				Feed curFeed = mSAXHandler.createBuzzReader(this, feedUrl);
				//do something with the feed (curFeed) here
				int nbNewBuzzIds = curFeed.mActivityList.size();
				textview.setText("Your current latitude: " + latitude + "\nYour current longitude: " + longitude+"\nThere is " + nbNewBuzzIds + " buzz(es) around you.");
			} catch (MalformedURLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
        }
        else{
        	textview.setText("Location information not available, please start a location service like Google Maps.");
        }
        setContentView(textview);
        dialog.cancel();
    }
}
